<?php

/**
 * @Author: 公共函数库
 * @Author: saipu
 * @Date:   2018-07-19 10:21:26
 * @Last Modified by:   saipu
 * @Last Modified time: 2019-04-09 15:48:24
 */

/**
 *  获取课程收藏数量
 * @param $course_class_id  课程id
 * @param object $list 返回数据
 * @Author: luyahe
 */
function sum_collect($course_class_id){
	$list = DB::select("select count(id) as count  from collect where course_class_id={$course_class_id}");
	return $list[0];
}

/**
 *  获取视频总集数
 * @param $course_class_id  课程id
 * @param object $list 返回数据
 * @Author: luyahe
 */
function sum_course($course_class_id){
	$list = DB::select("select count(id) as count  from course where course_class_id={$course_class_id}");
	return $list[0];
}

/**
 *  获取用户输出总内容
 * @param $user_id  课程id
 * @param object $list 返回数据
 * @Author: luyahe
 */
function sum_content($user_id){
	//课程总数
	$list = DB::select("select count(id) as count  from course_class where user_id={$user_id}");

	//文章总数
	return $list[0]->count;
}

/**
 *  获取标签相关文章数量
 * @param $course_class_id  课程id
 * @param object $list 返回数据
 * @Author: luyahe
 */
function sum_tag_course($tag_id){
	$list = DB::table("course_tag")->where('tag_id', $tag_id)->pluck('course_class_id')->toArray();
	if(empty($list)){
		return 0;
	}else{
		return count($list);
	}
}

/**
 * comment获取相应课程名称
 */

function get_course_name($id){
	$data = DB::table("course_class")->where("id",$id)->select("title")->get();
	return $data[0]->title;
}

/**
 * comment获取相应课程id
 */
function get_course_id($title){
	$data = DB::table("course_class")->where("title","like","%$title%")->select("id")->first();
	return $data->id;
}

/**
 *  获取客户收入信息
 * @param $user_id  用户id
 * @param object $list 返回数据
 * @Author: luyahe
 */
function sum_user_add($user_id){
	$list = DB::select("select ceil(sum(money)) as count  from finance_records where user_id={$user_id} and type='ADD'");
	if($list[0]->count){
		return $list[0]->count;
	}else{
		return 0;
	}
}

/**
 *  获取用户输出评论总数
 * @param $user_id  用户id
 * @param object $list 返回数据
 * @Author: luyahe
 */
function sum_comment($user_id){
	//课程总数
	$list = DB::select("select count(id) as count  from comments where user_id={$user_id}");

	//文章总数
	return $list[0]->count;
}
/**
 *  图片格式
 * @param array $text_type 返回数据
 * @Author: luyahe
/**获取上传文件类型
 * 
 */
function return_type(){
	$text_type  = array('.gif', '.jpg', '.png');
	return $text_type;
}

function get_course_class_img($course_class_id, $field="activity_url"){
	$item = DB::table("course_class_about")->where("course_class_id",$course_class_id)->select($field)->first();
	if($item){
		return $item->$field;
	}else{
		return false;
	}
}

/**
 * 获取用户名称
 */
function getUserName($id){
	$data = DB::table("users")->where("id",$id)->select("name","nickname","mobile")->first();
	return $data;
}


/**
 * 获取相应课程名称
 */

function get_course_name1($id){
	$data = DB::table("course_class")->where("id",$id)->select("title")->first();
	return $data->title;
}
/*
 *  获取文章类别信息
 * @param $type_ids  文章id
 * @param object $list 返回数据
 * @Author: luyahe
 * @time: 20180927
 */
function article_types($type_ids){
	$ids  = explode(',', $type_ids);
	$list = DB::table("type")->whereIn("id", $ids)->select("title")->get();
	if($list->count()){
		$str = "";
		foreach($list as $item){
			$str.= $item->title.",";
		}
		return trim($str, ",");
	}else{
		return '暂无';
	}
}

/**
 *  获取文章类别信息
 * @param $type_ids  文章id
 * @param object $list 返回数据
 * @Author: luyahe
 * @time: 20180927
 */
function article_tags($tag_ids){
	$ids  = explode(',', $tag_ids);
	$list = DB::table("tags")->whereIn("id", $ids)->select("title")->get();
	if($list->count()){
		$str = "";
		foreach($list as $item){
			$str.= $item->title.",";
		}
		return trim($str, ",");
	}else{
		return '暂无';
	}
}

/**
 * 获取文章标题
 */

function get_article_id($title){
	$data = DB::table("article")->where("title","like","%$title%")->select("id")->first();
	return $data->id;
}
function get_article_name($id){
	$data = DB::table("article")->where("id",$id)->select("title")->first();
	return $data->title;
}




/**
 * 赛普币调用  注册 完善
 * $userid 用户id
 * $rulesid 规则id
 * 规则id
 * 2-->唯一一次（新手任务）
 * 6-->购买课程返币
 * 7-->评价课程返币
 * 8-->分享课程（一天三次)
 * 9-->每日首次收藏，报名，学习，关注返币

 * $userid -- 用户id
 * $rulesid -- 规则id
 * $money 	-- 购买课程付款钱数
 * $courseId -- 购买课程id
 *购买课程返赛普币
 */
function courseSpb($userid,$rulesid,$courseid="0",$money="",$operator="0"){
		$newMoney = getM($rulesid);
		$unique = $newMoney->is_unique;

		if($unique == 2){
			if($money < 1){
				$money = 1;
			}
			$a1 = (float)$newMoney->value/100;
			$m1 = $a1 * $money *100;
			$m = (int)$m1;
			
		}else{
			$m = $newMoney->value;	
		}
		$condition['user_id'] = $userid;
		$condition['spb_rule_id'] = $rulesid;
		$condition['value'] = $m;
		if($rulesid == 11){
			$condition['tid'] = $courseid;
		}
		$condition['courseid'] = $courseid;
		$condition['created_at'] = date("Y-m-d H:i:s");
		$condition['operator'] = $operator;
		$count = course_over($userid,$rulesid,$unique,$courseid);

		
		if( $count == 0){//每日单次任务
			$data = DB::table("spb_records")->insert($condition);
			if($data){
				$update = DB::table("users")->where("id", '=', $userid)->increment("spb", $m);
			}
			return true;
		}elseif($count < 3 && $unique == 4){
			$data = DB::table("spb_records")->insert($condition);
			if($data){
				$update = DB::table("users")->where("id", '=', $userid)->increment("spb", $m);
			}
			return true;
		}elseif($count = 100){
			$data = DB::table("spb_records")->insert($condition);
			if($data){
				$update = DB::table("users")->where("id", '=', $userid)->increment("spb", $m);
			}
			return true;
		}else{//不限次数
			
			return false;
		}
}

/**
 * 获取相应规则对应的钱币
 */

function getM($rulesId){
	$data = DB::table("spb_rules")->where("id",$rulesId)->select("value","is_unique")->first();
	return $data;
}

/**
 * 购买收藏课程
 */
 
function course_over($userid,$rulesid,$unique = "",$courseId=""){
	if($unique == "5" || $unique == "4"){
		$data = DB::table("spb_records")->where("user_id",$userid)
										->where('spb_rule_id',$rulesid)
										->where("created_at","like",date('Y-m-d')."%")
										->select("count(*) as num")->count();
	}elseif($unique == "2" || $unique == "3"){
			$data = DB::table("spb_records")->where("user_id",$userid)
										->where('spb_rule_id',$rulesid)
										->where("courseid",$courseId)
										->select("count(*) as num")->count();
	}elseif($unique == "7"){
		$data = 100;
	}else{
		$data = DB::table("spb_records")->where("user_id",$userid)->where('spb_rule_id',$rulesid)->select("count(*) as num")->count();
	}
	
	
	return $data;
	
}
/**
 * 
 * 20181114 测试自动发送资源给百度熊掌号
 */
function post_baidu($id,$info="article"){
	if($info=='article'){
		$urls = array(
	        'http://m.saipubbs.com/article/detail/'.$id.'.html',
	    );
	}else{
		$urls = array(
	        'http://m.saipubbs.com/course/detail/'.$id.'.html',
	    );
	}
    
    $api = 'http://data.zz.baidu.com/urls?appid=1551964634744717&token=igXSBY6I8rkEv4pV&type=realtime';
    $ch = curl_init();
    $options =  array(
        CURLOPT_URL => $api,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => implode("\n", $urls),
        CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
    );
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    //print_r($result);
    logger()->info($result);
}


/**
 * 裂变者
 */
function is_fission($userid){
	$data = DB::table("users_attribute")->where("user_id",$userid)->select("is_fission")->first();
	if($data){
		return $data->is_fission;
	}else{
		return 0;
	}

}

/**
 * 查看分销打卡视频标题
 */
function dis_course($ids){
	$title = "";
	if($ids){
		$data = DB::select("select title  from dis_course where id in ($ids)");
		if($data){
			foreach($data as $item){
				$title .=$item->title.";  ";
			}
		}
	}
	return $title;
}



